<?php
/**
 * Created by JetBrains PhpStorm.
 * User: huan
 * Date: 14-3-21
 * Time: 上午9:23
 * To change this template use File | Settings | File Templates.
 */

/**
 C:\wamp\bin\php\php5.4.12\php.exe -f C:\phproot\rush\trunk\Cli\jd_search_rank_auto.php -c C:\wamp\bin\apache\apache2.4.4\bin\php.ini

/usr/local/bin/php -f /usr/home/koubei/Cli/jd_search_rank_auto.php
 */

set_time_limit(0);

define('ROOT_PATH', str_replace('Cli/'.basename(__FILE__), '', str_replace('\\', '/', __FILE__)));

require_once(ROOT_PATH."Cls/const.php");
require_once(ROOT_PATH."Cls/common.php");
require_once(ROOT_PATH."Cls/db.php");
require_once(ROOT_PATH."Cls/lib_jd.php");
require_once(ROOT_PATH."Cls/lib_jd_search.php");
require_once(ROOT_PATH."Cls/curl.php");
require_once(ROOT_PATH."Cls/cls_jd_search.php");

$db = new DB();

do{

    //待查询的商品
    $search_goods = get_un_search_order_goods();

    //要查询的数量
    $goods_count = count($search_goods);

    //休息
    cli_out('');
    cli_out('');
    cli_out('');
    cli_out('');
    cli_out('********************');
    cli_out('goods_count:'.$goods_count.' wait to search rank');

    if(!empty($search_goods)){

        foreach($search_goods as $i => $g){

            $item_id    = $g['item_id'];
            $keywords   = $g['keywords'];

            cli_out("task: (".($i + 1 )." / {$goods_count})");
            cli_out('item_id:'.$item_id.' keywords:'.$keywords.' search rank,please wait...');

            $cJs = new JdSearch($item_id,$keywords);
            $rank = $cJs->get_rank();

            cli_out('rank:'.$rank);

            cli_out('update item_id:'.$item_id.' search rank:'.$rank);

            //修改排名
            $aft_row = goods_update_search_rank($item_id,$keywords,$rank);

            cli_out('update search rank, affect_rows : '.$aft_row);

            cli_out('sleep 3 seconds');
            sleep(3);
        }
    }else{
        //休息
        cli_out('sleep 20 seconds');
        cli_out('********************');
        sleep(20);
    }

}while(true);

